package com.world.business.treasure.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.treasure.domain.Treasure;
import com.world.business.treasure.domain.query.TreasureAddRequest;
import com.world.business.treasure.domain.query.TreasureRequest;
import com.world.business.treasure.domain.query.TreasureUpdateRequest;
import com.world.business.treasure.domain.vo.TreasureVO;
import com.world.business.treasure.mapper.TreasureMapper;
import com.world.business.treasure.service.ITreasureService;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 灵兽/灵植 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-31
 */
@Service
public class TreasureServiceImpl extends ServiceImpl<TreasureMapper, Treasure> implements ITreasureService {

    @Override
    public List<TreasureVO> list(TreasureRequest request) {
        LambdaQueryWrapper<Treasure> queryWrapper = Wrappers.lambdaQuery(Treasure.class);
        queryWrapper.eq(Treasure::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.like(StrUtil.isNotBlank(request.getName()), Treasure::getName, request.getName());
        List<Treasure> treasures = this.baseMapper.selectList(queryWrapper);
        List<TreasureVO> vos = BeanUtil.copyToList(treasures, TreasureVO.class);
        return vos;
    }

    @Override
    public Long add(TreasureAddRequest request) {
        LambdaQueryWrapper<Treasure> queryWrapper = Wrappers.lambdaQuery(Treasure.class);
        queryWrapper.eq(Treasure::getName, request.getName());
        Treasure one = this.baseMapper.selectOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)) {
            throw new BusinessException("名称不能重复！");
        }
        Treasure treasure = BeanUtil.copyProperties(request, Treasure.class);
        treasure.setCreateTime(LocalDateTime.now());
        treasure.setCreateBy(SecurityUtils.getLoginUser().getName());
        treasure.setCreateById(SecurityUtils.getLoginUser().getId());
        treasure.setDelFlag(DelFlagEnum.NORMAL.getCode());
        this.baseMapper.insert(treasure);
        return treasure.getId();
    }

    @Override
    public void update(TreasureUpdateRequest request) {
        Treasure treasure = this.baseMapper.selectById(request.getId());
        if (BeanUtil.isEmpty(treasure)) {
            throw new BusinessException("灵兽或者灵植不存在！");
        }
        LambdaUpdateWrapper<Treasure> updateWrapper = Wrappers.lambdaUpdate(Treasure.class);
        updateWrapper.eq(Treasure::getId, request.getId());
        updateWrapper.set(Treasure::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Treasure::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Treasure::getUpdateTime, LocalDateTime.now());

        updateWrapper.set(StrUtil.isNotBlank(request.getName()), Treasure::getName, request.getName());
        updateWrapper.set(StrUtil.isNotBlank(request.getDescription()), Treasure::getDescription, request.getDescription());
        updateWrapper.set(request.getRaceId() != null, Treasure::getRaceId, request.getRaceId());
        updateWrapper.set(request.getOwner() != null, Treasure::getOwner, request.getOwner());
        updateWrapper.set(StrUtil.isNotBlank(request.getRarity()), Treasure::getRarity, request.getRarity());
        updateWrapper.set(request.getBirthday() != null, Treasure::getBirthday, request.getBirthday());
        updateWrapper.set(request.getDieathday() != null, Treasure::getDieathday, request.getDieathday());
        updateWrapper.set(StrUtil.isNotBlank(request.getStatus()), Treasure::getStatus, request.getStatus());
        updateWrapper.set(StrUtil.isNotBlank(request.getType()), Treasure::getType, request.getType());
        updateWrapper.set(StrUtil.isNotBlank(request.getSource()), Treasure::getSource, request.getSource());
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public void delete(Long id) {
        Treasure treasure = this.baseMapper.selectById(id);
        if (BeanUtil.isEmpty(treasure)) {
            throw new BusinessException("灵兽或者灵植不存在！");
        }
        LambdaUpdateWrapper<Treasure> updateWrapper = Wrappers.lambdaUpdate(Treasure.class);
        updateWrapper.eq(Treasure::getId, id);
        updateWrapper.set(Treasure::getDelFlag, DelFlagEnum.DELETE.getCode());
        updateWrapper.set(Treasure::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Treasure::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Treasure::getUpdateTime, LocalDateTime.now());
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public TreasureVO get(Long id) {
        Treasure treasure = this.baseMapper.selectById(id);
        if (BeanUtil.isEmpty(treasure)) {
            throw new BusinessException("灵兽或者灵植不存在！");
        }
        return BeanUtil.copyProperties(treasure, TreasureVO.class);
    }
}
